<template>
  <div class="role">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card mt-15">
          <div class="home-form">
            <el-form ref="ruleForm" label-width="80px" @submit.native.prevent>
              <el-row :gutter="10">
                <el-col :span="4" v-if="tenantId === superTenantId">
                  <el-form-item label="选择租户" prop="selectTenantId">
                      <el-select v-model="selectTenantId" placeholder="请选择租户" style="width:100%" clearable :disabled="$parent.actionType == 'edit'" size="mini">
                        <el-option
                          v-for="item in tenantArr"
                          :key="item.id"
                          :label="item.name"
                          :value="item.id">
                        </el-option>
                      </el-select>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="4">
                  <el-form-item label="类型" prop="searchKey">
                    <el-select v-model.trim="searchKey" placeholder="请选择" size="mini">   
                      <el-option v-for="(item, index) in searchArr" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col> -->
                <el-col :span="4">
                  <el-form-item label="角色名称" prop="name">
                    <el-input  placeholder="请输入角色名称" size="mini" v-model.trim="name" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="2">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch" class="search-btn">搜索</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
        <el-card class="box-card">
          <!-- 头部 -->
          <div slot="header" class="clearfix fix-lh">
            <span>角色管理</span>
            <div class="btn-list">
              <el-button size="mini" @click="handleAdd" v-hasPermission="permission.add">新增</el-button>
              <!-- <el-button size="mini" @click="handleDeleteAll" v-hasPermission="permission.roles.postRolesDelete">批量删除</el-button> -->
            </div>
          </div>
          <div class="home-body">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              @select-change="handleSelectChange"
              :isShowPage="true">
              <template slot-scope="scope" slot="create_time">
                <span>{{ scope.row.create_time | timeFix }}</span>
              </template>
              <!-- 操作按钮 -->
              <el-table-column fixed="right" slot="actionMenu" label="操作" align="center" header-align="center" width="260">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="handleEdit(scope.row)" v-hasPermission="permission.edit" v-if="scope.row.code !== 'superAdmin'">修改</el-button>
                  <el-button type="text" size="small" @click="handleDelete(scope.row)" v-hasPermission="permission.delete" v-if="scope.row.code !== 'superAdmin'">删除</el-button>
                  <el-button type="text" size="small" @click="handleMenu(scope.row)" v-hasPermission="permission.menu">菜单权限</el-button>
                  <el-button type="text" size="small" @click="handlePermission(scope.row)" v-hasPermission="permission.action">操作权限</el-button>
                </template>
              </el-table-column>
            </cvue-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 编辑弹窗 -->
    <role-edit
      @closeDialog="closeDialog('edit')"
      @confirmDialog="confirmDialog('edit')"
      @handleClose="closeDialog('edit')"
      v-if="dialogRoleEdit"
      :dialogVisible="dialogRoleEdit">
    </role-edit>
    <!-- 菜单管理弹窗 -->
    <role-menu-tree
      @closeDialog="closeDialog('menuTree')"
      @confirmDialog="confirmDialog('menuTree')"
      @handleClose="closeDialog('menuTree')"
      v-if="dialogRoleMenuTree"
      :dialogVisible="dialogRoleMenuTree">
    </role-menu-tree>
     <!-- 权限管理弹窗 -->
    <role-permission
      @closeDialog="closeDialog('permission')"
      @confirmDialog="confirmDialog('permission')"
      @handleClose="closeDialog('permission')"
      v-if="dialogRolePermission"
      :dialogVisible="dialogRolePermission">
    </role-permission>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import cvueTable from '@/components/cvue-table'
import roleEdit from './roleEdit'
import roleMenuTree from './roleMenuTree'
import rolePermission from './rolePermission'
import {getClientHeight} from '@/util/tool'
export default {
  name: 'role',
  components: {
    cvueTable,
    roleEdit,
    roleMenuTree,
    rolePermission
  },
  data () {
    return {
      tablePage: 1,
      tableLoading: false,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100]
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        selected: false,
        cloumn: [
          { prop: 'id', label: 'ID', align: 'center', sortable: true },
          { prop: 'name', label: '角色名', align: 'center', sortable: true },
          { prop: 'code', label: 'Code', align: 'center', sortable: true },
          { prop: 'create_time', label: '创建时间', align: 'center', sortable: true, slotStatus: true }
        ]
      },
      tableData: [
        // { name: 'ddd', code: 'sss', order: 1, status: 1 }
      ],
      dialogRoleEdit: false,
      dialogRoleMenuTree: false,
      dialogRolePermission: false,
      actionType: 'add',
      searchArr: [
        {label: '角色名', value: 'name'}
      ],
      name: '',
      selectName: '',
      rowData: {},
      selectIds: [],
      tenantArr: [],
      selectTenantId: '',
      selectTid: ''
    }
  },
  computed: {
    height () {
      var height = getClientHeight() - 170
      return height + 'px'
    },
     ...mapGetters(['token', 'tenantId'])
  },
  mounted () {
    // this.handleList(this.selectName, this.selectTid)
    // console.log(this.$route)
    if (this.tenantId === this.superTenantId) {
      this.getTenantArr()
    } else {
      this.selectTid = this.tenantId
      this.handleList(this.selectName, this.selectTid)
    }
  },
  methods: {
    // 新增
    handleAdd () {
      this.actionType = 'add'
      this.dialogRoleEdit = true
    },
    // 编辑
    handleEdit (row) {
      this.actionType = 'edit'
      this.dialogRoleEdit = true
      this.rowData = row
    },
    // 删除
    handleDelete (row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        // console.log(params)
        this.$store.dispatch('manage/RoleDelete', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.tablePage = 1
          this.handleList(this.selectName, this.selectTid)
        }).catch((err) => {
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    // 删除
    handleDeleteAll () {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.selectIds.length == 0) {
          this.$message({type: 'error', message: '至少选择一条'})
          return
        }
        var params = this.selectIds
        this.$store.dispatch('manage/DeleteRoleByIds', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.tablePage = 1
          this.handleList(this.selectName, this.selectTid)
        }).catch((err) => {
          this.$message({
            type: 'error',
            message: err.msg
          })
        })
      }).catch(() => {
      })
    },
    // 改变pagesize
    handleSizeChange (val) {
      this.page.pageSize = val
      this.handleList(this.selectName, this.selectTid)
    },
    // 改变页码
    handleCurrentChange (val) {
      this.tablePage = val
      this.handleList(this.selectName, this.selectTid)
    },
    // 表格选择
    handleSelectChange (val) {
      // console.log(val)
      var arr = []
      for (var i = 0; i < val.length; i++) {
        arr.push(val[i].id)
      }
      this.selectIds = arr
      // console.log(this.selectIds)
    },
    // 列表接口
    handleList (name, tenantId) {
      this.tableLoading = true
      var params = {
        page: this.tablePage,
        limit: this.page.pageSize,
        name: name,
        tenantId: tenantId
      }
      this.$store.dispatch('manage/RoleGet', params).then(res => {
        // console.log(res)
        var data = res.data.datas
        this.tableData = data
        this.page.total = res.data.count
        this.page.currentPage = this.tablePage
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        // this.$message({ message: err.msg, duration: 2000, type: 'error' })
        this.tableLoading = false
      })
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogRoleEdit = false
      }
      if (params == 'menuTree') {
        this.dialogRoleMenuTree = false
      }
      if (params == 'permission') {
        this.dialogRolePermission = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogRoleEdit = false
        // this.tablePage = 1
        this.handleList(this.selectName, this.selectTid)
      }
      if (params == 'menuTree') {
        this.dialogRoleMenuTree = false
      }
      if (params == 'permission') {
        this.dialogRolePermission = false
      }
    },
    handleSearch () {
      if (this.tenantId === this.superTenantId) {
        this.selectTid = this.selectTenantId
        if (this.selectTid == '') {
          this.$message({ message: '请选择租户', duration: 2000, type: 'warning' })
          return
        }
      } else {
        this.selectTid = this.tenantId
      }
      this.selectName = this.name
      this.tablePage = 1
      this.handleList(this.selectName, this.selectTid)
    },
    // 菜单管理
    handleMenu (row) {
      // console.log(row)
      this.rowData = row
      this.dialogRoleMenuTree = true
    },
    // 权限管理
    handlePermission (row) {
      this.rowData = row
      this.dialogRolePermission = true
    },
    // 获取租户列表
    getTenantArr () {
      var params = {
        code: '',
        name: ''
      }
      this.$store.dispatch('tenant/TenantSelect', params).then(res => {
        // console.log(res)
        this.tenantArr = res.data.filter((item, index, array) => {
          return item.deleted === 0 && item.enabled
        })
      })
    }
  }
}
</script>

<style lang="scss">
.role {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
